//使用于loginPage.jsp, registerPage.jsp, backStageDecorator.jsp. frontSideDecorator.jsp
function changeCss(event) {
	var id = $(event).attr("cid");
	var title = id.split("-");
	setStyleSheet(title[1]);
}
// ref: alternate stylesheet
function setStyleSheet(title) {
	var link_list = document.getElementsByTagName("link");
	if (link_list) {
		for (var i = 0; i < link_list.length; i++) {
			// 要找到所有link中rel属性值包括style的，也就是包括stylesheet和alternate stylesheet;
			if (link_list[i].getAttribute("title") != null) {
				// 将符合条件的link的disabled的属性设为true，都改为禁用；
				console.log(link_list[i].getAttribute("title"));
				link_list[i].disabled = true;
				// 然后判断link标签中的title属性，找到我们需要替换的css文件
				// 找到后将该link的disabled改为启用；
				console.log(title + 'model');
				if (link_list[i].getAttribute("title") == title + 'model') {
					console.log(link_list[i]);
					link_list[i].disabled = false;
				}
			}
		}
	}
}

function showPsw() {
	var type = $("#scau-login-secret").attr("type");
	if (type == "password") {
		$("#scau-login-secret").attr("type", "text");
		var img = $("#scau-login-eye");
		img.attr("src", "/<c:get select="$projectName"/>/images/icons/Openeyes.png");
	} else {
		$("#scau-login-secret").attr("type", "password");
		var img = $("#scau-login-eye");
		img.attr("src", "/<c:get select="$projectName"/>/images/icons/eye.png");
	}
}

function showPsw1() {
	var type = $("#scau-login-secret1").attr("type");
	if (type == "password") {
		$("#scau-login-secret1").attr("type", "text");
		var img = $("#scau-login-eye1");
		img.attr("src", "/<c:get select="$projectName"/>/images/icons/Openeyes.png");
	} else {
		$("#scau-login-secret1").attr("type", "password");
		var img = $("#scau-login-eye1");
		img.attr("src", "/<c:get select="$projectName"/>/images/icons/eye.png");
	}
}

function chooseSource() {
	$(".scau-successsave").css("display", "none");
	$(".scau-choosesource").css("display", "");
}
function chooseBack() {
	$(".scau-choosesource").css("display", "none");
}
//function uploadExcel() {
//	$(".scau-choosesource").css("display", "none");
//	$(".scau-uploadexcel").css("display", "");
//}
function uploadBack() {
	$(".scau-uploadexcel").css("display", "none");
	$(".scau-choosesource").css("display", "");
}


var fileHash;

function uploadExcel(){
	var flag;
	var list = new FormData();
	var file = $('#fileChoose')[0].files[0];
	if(file == null || file == undefined){
		alert("请先选择文件！");
		return;
	}
	var fileName = file.name;
	var fileExt = fileName.split('.')[1];
	var sourceId = $('#sourceSelect').val();
	if(sourceId == -1){
		alert("请选择来源！");
		return;
	}
	if (fileExt != 'xls' && fileExt != 'xlsx') {
		alert("上传的文件不是Excel文件！请重新选择文件！");
		flag = false;
	}
	list.append('file', file);
	$.ajax({
		url : 'uploadExcel?sourceId='+sourceId,
		data : list,
		type : 'post',
		async : false,
		processData : false,
		contentType : false,
		success : function(data) {
			console.log(data);
			hash = data.split("=")[1];
			if (data == "error"){
				alert("该文件已被入库！请选择其他文件");
				flag = false;
			}
			else {
				fileHash = data;
				flag = true;
			}
		}
	})
	return flag;
}

$("#addsource").click(function() {
	window.location.href = 'addSource';
});

/*
 * var sourceid;
 * 
 * function uploadExcel() { $.ajax({ url : 'getSourceId', type : 'post',
 * dataType : 'json', data : { "sourceName" : $("#choose").val() }, success :
 * function(data) { $(".scau-choosesource").css("display", "none");
 * $(".scau-uploadexcel").css("display", ""); sourceid = data; //
 * window.location.href='uploadExcel?id=' + data; } }) };
 */
function autoMatch(){
	if(uploadExcel()){
		var sourceId = $('#sourceSelect').val();
		$.ajax({
			url : 'autoMatch?hash='+fileHash+"&&sourceId="+sourceId,
			type : 'get',
			async : false,
			dataType : 'json',
			success : function(data) {
				
				console.log(data);
				$("#ruleSelect").html('');
				$("#ruleSelect").append("<option value='-1'>数据已填充，请选择</option>");
				if(data.length > 0){
					for (i in data) {
						
						current = data[i];
						$("#ruleSelect").append(
							"<option value='" + current.id + "'>规则编号" + current.id
									+ "</option>")
					}
					alert("数据库中可能匹配的规则已经填充到下拉列表中!");
				}else{
					alert("该来源下没有可能匹配的规则！请新建规则！");
				}
				
			}
		})
	}
}
function saveFileRecord(ruleId) {
	
	//检查之前是否有上传过文件，如果没有则上传
	if(!fileHash || typeof(fileHash) == 'undefined' || fileHash == null){
		if(!uploadExcel()){
			return;
		}
	}
	var ruleId = $("#ruleSelect").val();
	$.ajax({
		url : 'storeExcel?hash=' + fileHash + '&&ruleId=' + ruleId,
		type : 'post',
		success : function() {
			alert("Excel文件已经成功上传到服务器！请返回列表页面对文件进行入库操作！");
			window.location.href='listExcels';
		}
	})
}

function changesource(obj) {
	var sourceid = $(obj).val();
	$.ajax({
		url : 'getRules?sourceId=' + sourceid,
		type : 'get',
		async : false,
		dataType : 'json',
		success : function(data) {
			console.log(data);
			$("#ruleSelect").html('');
			$("#ruleSelect").append("<option value='-1'>请选择...</option>");
			for (i in data) {
				current = data[i];
				$("#ruleSelect").append(
						"<option value='" + current.id + "'>规则编号" + current.id
								+ "</option>")
			}
		}
	})
}

function changetable(obj) {
	var ruleid = $(obj).val();
	$.ajax({
		url : 'getRuledetails?ruleId=' + ruleid,
		type : 'get',
		async : false,
		dataType : 'json',
		success : function(data) {
			console.log(data);
			$("#confirmrule-tbody").html('');
			for (i in data) {
				one = data[i];
				$("#confirmrule-tbody").append(
						"<tr><td>" + one.colname + "</td><td>" + one.tablename
								+ "</td><td>" + one.field + "</td></tr>")
			}
		}
	})
}

function goToChooseRule() {
	//检查之前是否有上传过文件，如果没有则上传
	if(!fileHash || typeof(fileHash) == 'undefined' || fileHash == null){
		if(!uploadExcel()){
			return;
		}
	}
	window.location.href = 'chooseRule?' + "hash=" + fileHash;
}

function goToImportXls() {
	var ruleid = $("#ruleSelect").val();
	var param = decodeURI(window.location.href);
	hash = param.split("=")[1];
	$.ajax({
		url : 'importXls?hash=' + hash + '&&ruleId=' + ruleid,
		type : 'post',
		success : function() {
			$(".scau-successsave").css("display", "");
		}
	})
}

function saveExcelContent(excelId) {
	$.ajax({
		type : 'post',
		url : '/<c:get select="$projectName"/>/importXls?excelId=' + excelId,
		success : function() {
			alert("数据入库成功！");
			window.location.href="listExcels"
		}
	})
}

function cleanExcel(excelId) {
	$.ajax({
		type : 'post',
		url : '/<c:get select="$projectName"/>/cleanExcel?excelId=' + excelId,
		success : function() {
			alert("数据删除成功！");
			window.location.href="listExcels"
		}
	})
}

function saveBack() {
	window.location.href = 'listExcels';
}

function seeExcel() {
	window.location.href = 'showAllExcel';
}

function addSource() {
	window.location.href = 'newSource';
}

function chooseruleBack() {
	$(".scau-chooserule").css("display", "none");
	$(".scau-choosesource").css("display", "");
}

function chooseruleOk() {
	$.ajax({
		url : 'createSource',
		type : 'post',
		dataType : 'json',
		data : {
			"sourceName" : $("#add").val()
		},
		success : function(data) {
			window.location.href = 'adapter';
		}
	})
}